var SFXManager = require("SFXManager");

var OptionManager = cc.Class({
    extends: cc.Component,

    properties: {
        soundSwitch: cc.Node,
		BGMVolumeNode: cc.Node,
		SFXVolumeNode: cc.Node,
		isOpenSound: true,
		BGMVolume: null,
		SFXVolume: null,

    },

	statics: {
		instance: null,
	},
	ctor() {
		OptionManager.instance = this;
	},

    // LIFE-CYCLE CALLBACKS:

    onLoad () {
		let storage = window.localStorage;
		this.BGMVolume = storage.getItem("BGMVolume");
		this.SFXVolume = storage.getItem("SFXVolume");
		this.BGMVolumeNode.getChildByName("Slider").getComponent(cc.Slider).progress = this.BGMVolume;
		this.SFXVolumeNode.getChildByName("Slider").getComponent(cc.Slider).progress = this.SFXVolume;
	},

    start () {
    },

	switchSound () {
		this.isOpenSound = !this.isOpenSound;
		if (this.isOpenSound == false) {
			this.BGMVolume = 0;
			this.SFXVolume = 0;
			this.saveVolume("BGMVolume");
			this.saveVolume("SFXVolume");
			this.BGMVolumeNode.getChildByName("Slider").getComponent(cc.Slider).progress = this.BGMVolume;
			this.SFXVolumeNode.getChildByName("Slider").getComponent(cc.Slider).progress = this.SFXVolume;
		}
	},

	setBGMVolume () {
		this.BGMVolume = this.BGMVolumeNode.getChildByName("Slider").getComponent(cc.Slider).progress;
		this.saveVolume("BGMVolume");
		SFXManager.instance.setBGMVolume();
	},

	setSFXVolume () {
		this.SFXVolume = this.SFXVolumeNode.getChildByName("Slider").getComponent(cc.Slider).progress;
		this.saveVolume("SFXVolume");
	},

	returnMainMenu () {
		this.node.active = false;
	},

	saveVolume(type) {
		let storage = window.localStorage;
		if (type == "BGMVolume") {
			storage.setItem(type, this.BGMVolume);
		}
		if (type == "SFXVolume") {
			storage.setItem(type, this.SFXVolume);
		}
	}
});
module.exports = OptionManager;
